/******************************init****************************/

function queryDocumentInfo_documentInfo(page) {
	sys_loading();
	var parm = $('#queryDocumentInfoForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('documentInfo-query');
	sys_record_parm('documentInfo',parm);
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#documentInfoResultList').html(result);
		}
	});
}


/******************************userInit****************************/
function selectUser_documentInfo(k,obj) {
	var che = obj.attr('checked');
	var a = '';
	if ('checked' == che || che) {
		var i = $('#t').val();
		var j = obj.val();
		if (i == j) {
			$('div#ptypeObjList_' + k).find('input.pc').each(function () {
				a += $(this).val() + ',';
			});
			if (a.length > 0) {
				a = a.substring(0, a.length - 1);
			}
		}
	}
	var parm = {"p":k, 'a':a};
	var url = create_url("documentInfo-aidInit");
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'1000px', height:'700px', open:true});
}

function aidQueryUser_documentInfo(page) {
	var oldoc = $('#old_oc').val();
	var suc = '';
	$('#selectedListBody').find('.run').each(function () {
		suc += $(this).val() + ',';
	});
	suc += oldoc;
	if (suc.length > 0 && oldoc.length == 0) {
		suc = suc.substring(0, suc.length - 1);
	}
	var parm = $('#aidQueryForm').serialize() + '&suc=' + suc + '&pageSize=10' + (page != null ? ('&page=' + page) : '');
	var url = create_url('documentInfo-aidList');
	sys_loading();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#aidQueryResultList').html(result);
		}
	});
}

function chooseUser_documentInfo() {
	var k = $('#u_p').val();
	var template = $('#tempListDiv').find('.rep');
	var ListTd = $('#ptypeObjList_' + k);
	var i = 0;
	var p = $('#t').val();
	if (p != 5) {
		ListTd.empty();
	}
	$('#t').val(5);
	$('#selectedListBody').find('.rep').each(function () {
		var rep = template.clone(true);
		rep.find('.title').html($(this).find('.un').val()).end()
			.find(".pc").attr({"name" : "list[" + k + "].publicList[" + i + "].pc", "value":$(this).find('.run').val()}).end()
			.find('.ucs').attr({'realName':"list[" + k + "].publicList[" + i + "].mc"}).end();
		ListTd.append(rep);
		i++;
	});
	
	ListTd.parents('.td-rep').css('display','');
	$.fn.colorbox.close();
}


function addUser_documentInfo(code, realName, on) {
	var tr = $('#tempTable').find('.rep').clone(true);
	tr.find('.rn').html(realName).end()
		.find('.on').html(on).end()
		.find('.run').val(code).end()
		.find('.un').val(realName).end()
	$('#selectedListBody').append(tr);
	aidQueryUser_documentInfo($('#aidQueryUserPageTxt').val());
}

/*******************organInit*************/
function selectOrgan_documentInfo(k,obj) {
	var che = obj.attr('checked');
	var a = '';
	if ('checked' == che || che) {
		var i = $('#t').val();
		var j = obj.val();
		if (i == j) {
			$('div#ptypeObjList_' + k).find('input.pc').each(function () {
				a += $(this).val() + ',';
			});
			
			if (a.length > 0) {
				a = a.substring(0, a.length - 1);
			}
		}
	}
	var parm = {"p":k, 'a':a};
	var url = create_url("documentInfo-aidInit1");
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'1000px', height:'700px', open:true});
}

function aidQueryOrgan_documentInfo(page) {
	// 获取已选择机构编号并拼接为字符串
	var oldoc = $('#old_oc').val();
	var suc = '';
	$('#selectedListBody').find('.oc').each(function () {
		suc += $(this).val() + ',';
	});
	suc += oldoc;
	if (suc.length > 0 && oldoc.length == 0) {
		suc = suc.substring(0, suc.length - 1);
	}
	var parm = $('#aidQueryForm').serialize() + '&suc=' + suc + '&pageSize=10' + (page != null ? ('&page=' + page) : '');
	var url = create_url('documentInfo-aidList1');
	sys_loading();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#aidQueryResultList').html(result);
		}
	});
}

function addOrgan_documentInfo(code, name) {
	var tr = $('#tempTable').find('.rep').clone(true);
	tr.find('.n').html(name).end()
		.find('.oc').val(code).end()
		.find('.on').val(name).end();
	
	$('#selectedListBody').append(tr);
	aidQueryOrgan_documentInfo($('#aidQueryOrganPageTxt').val());
}


function chooseOrgan_documentInfo() {
	var k = $('#og_o').val(); 
	var p = $('#t').val();
	var template = $('#tempListDiv').find('.rep');
	var ListTd = $('#ptypeObjList_' + k);
	var i = 0;
	if (p != 3) {
		ListTd.empty();
	}
	$('#t').val(3);
	$('#selectedListBody').find('.rep').each(function (e) {
		var rep = template.clone(true);
		var ocode = $(this).find('.oc').val();
		var oname = $(this).find('.on').val();
		rep.find('.title').html(oname).end()
			.find(".pc").attr({"name" : "list[" + k + "].publicList[" + i + "].pc", "value":$(this).find('.oc').val()}).end()
			.find('.ucs').attr({'name':"list[" + k + "].publicList[" + i + "].mc"}).end();
		ListTd.append(rep);
		i++;
	});
	ListTd.parents('.td-rep').css('display','');
	$.fn.colorbox.close();
}



$(".txt_organ_name").keydown(function(event) {
	if (event.keyCode == '13') {
		aidQueryOrgan_documentInfo();
		return false;
	}
});

/*******************roleInit*************/

function aidQueryRole_documentInfo(page) {
	// 获取已选择角色编号并拼接为字符串
	var oldoc = $('#old_oc').val();
	var suc = '';
	$('#selectedListBody').find('.rc').each(function () {
		suc += $(this).val() + ',';
	});
	suc += oldoc;
	if (suc.length > 0 && oldoc.length == 0) {
		suc = suc.substring(0, suc.length - 1);
	}
	
	var parm = $('#aidQueryForm').serialize() + '&suc=' + suc + '&pageSize=10' + (page != null ? ('&page=' + page) : '');
	var url = create_url('documentInfo-aidList2');
	sys_loading();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#aidQueryResultList').html(result);
		}
	});
}

function addRole_documentInfo(code, name) {
	var tr = $('#tempTable').find('.rep').clone(true);
	tr.find('.n').html(name).end()
		.find('.rc').val(code).end()
		.find('.rn').val(name).end();
	
	$('#selectedListBody').append(tr);
	aidQueryRole_documentInfo($('#aidQueryRolePageTxt').val());
}




function chooseRole_documentInfo() {
	var ro = $('#ro_r').val();
	var template = $('#tempListDiv').find('.rep');
	var ListTd = $('#ptypeObjList_' + ro);
	var i = 0;
	var p = $('#t').val();
	if (p != 4) {
		ListTd.empty();
	}
	$('#t').val(4);
	$('#selectedListBody').find('.rep').each(function (e) {
		var rep = template.clone(true);
		var ocode = $(this).find('.rc').val();
		var oname = $(this).find('.rn').val();
		rep.find('.title').html(oname).end()
			.find(".pc").attr({"name" : "list[" + ro + "].publicList[" + i + "].pc", "value":$(this).find('.rc').val()}).end()
			.find('.suc').attr({'name':"list[" + ro + "].publicList[" + i + "].mc"}).end();
		ListTd.append(rep);
		i++;
	});
	ListTd.parents('.td-rep').css('display','');
	$.fn.colorbox.close();
}

function selectRole_documentInfo(k,obj) {
	var che = obj.attr('checked');
	var a = '';
	if ('checked' == che || che) {
		var i = $('#t').val();
		var j =obj.val();
		if (i == j) {
			$('div#ptypeObjList_' + k).find('input.pc').each(function () {
				a += $(this).val() + ',';
			});
			
			if (a.length > 0) {
				a = a.substring(0, a.length - 1);
			}
		}
	}
	var parm = {"p":k, 'a':a};
	var url = create_url("documentInfo-aidInit2");
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'1000px', height:'700px', open:true});
}

$(".txt_role_name").keydown(function(event) {
	if (event.keyCode == '13') {
		aidQueryRole_documentInfo();
		return false;
	}
});


/*************edit*****************/
function deleteRole_documentInfo(btn) {
	aidQueryRole_documentInfo($('#aidQueryRolePageTxt').val());
	
}
function deleteOrgan_documentInfo(btn) {
	btn.parents('.rep').remove();
	aidQueryOrgan_documentInfo($('#aidQueryOrganPageTxt').val());
}
function deleteUser_documentInfo(btn) {
	btn.parents('.rep').remove();
	aidQueryUser_documentInfo($('#aidQueryUserPageTxt').val());
}
function deleteSelectedItem_documentInfo(btn) {
	btn.parents('.rep').remove();
}
function addFilesUploadEvent_documentInfo() {
	var url = create_url('documentInfo-uploads');
	var btn = $('#uploadFilesBtn');
	new AjaxUpload(btn, {
		action:url,
		name:'files',
		multiple: true,
		responseType: 'json',
		onSubmit : function(file, ext) {
			if (ext && /^(zip|rar|txt|doc|docx|xls|xlsx|ppt|pptx|pdf|jpg|png|jpeg|gif|bmp|ZIP|RAR|TXT|DOC|DOCX|XLS|XLSX|PPT|PPTX|PDF|JPG|PNG|JPEG|GIF|BMP)$/.test(ext)){
				this.setData({
					'info':'文件类型正确'
				});
			} else {
				sys_msg('文件类型错误', '只能上传类型为：zip、rar、txt、doc、docx、xls、xlsx、ppt、pptx、pdf、及图片文件!');
				return false;               
			}
			$('#uploads_span').html(sys_loading_img);
			this.disable();
		},
		onComplete: function(files, response){
			
			if ('error' == response.result)
				sys_msg('失败', '上传文件失败，请检查文件名是否正确!');
			else if ('"{\"result\":\"file_type_error\"}"' == response.result)
				sys_msg('文件类型错误', '只能上传类型为：JGP、GIF、BMP、ICO、PNG的文件!');
			else {
				var code = "";
				$.each(response.list, function(i) {
					code += this.code + ",";
				});
				sys_load_handle('documentInfo-edit_' + code.substring(0, code.length - 1));
				
				this.enable();
			}  
		}
	});
}

function uploadsDocumentInfo_documentInfo() {
	var parm = $('#queryDocumentInfoForm').serialize();
	var url = create_url('documentInfo-edit');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if ('success' == result) {
				sys_msg('操作成功!');
				sys_load_handle('documentInfo');
			}
		}
	});
}

function saveDocumentInfo_documentInfo() {
	var a = $('.dn').val();
	if (a == null || a == '') {
		sys_msg('类别不能为空哦！');
		return false;
	}
	var parm = $('#editDocumentInfoForm').serialize();
	var url = create_url('documentInfo-save');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if (result == 'success') {
				sys_load_handle('documentInfo');
			} else {
				sys_msg('操作失败!');
			}
		}
	});
}

function download_documentInfo(path, code) {
	window.location.href = path + 'm/documentInfo-download_' + code + ".html";
}

function delete_documentInfo(btn) {
	btn.parents('.rep').remove();
}

